class Cache {
private:
    std::unordered_map<std::string, std::weak_ptr<Data>> cache;
    
public:
    std::shared_ptr<Data> getData(const std::string& key) {
        auto it = cache.find(key);
        if (it != cache.end()) {
            auto data = it->second.lock();  // 尝试获取强引用
            if (data) {
                return data;  // 缓存命中且对象仍存在
            } else {
                cache.erase(it);  // 对象已销毁，清理缓存项
            }
        }
        
        // 创建新数据并缓存
        auto newData = std::make_shared<Data>();
        cache[key] = newData;  // 自动转换为 weak_ptr
        return newData;
    }
};

class A{
public:
    A(){
        std::cout << "A" << std::endl;
    }
    int a;
    ~A(){
        std::cout << "~A" << std::endl;
    }
};
int main() {
    std::vector<std::shared_ptr<A>> vec;
    for (int i = 0; i < 10; ++i) {
        vec.push_back(std::make_shared<A>());
    }
    {
        shared_ptr<A> a = vec[0];
        vec.pop_front();
        std::cout << a.use_count() << std::endl;
    }

    return 0;
}